// 引入node-schedule
const schedule = require('node-schedule');

const User = require("../../../models/User/User");

async function hpreply() {
    //每分钟的第分钟定时执行一次:
    let rule = new schedule.RecurrenceRule();
    rule.second = 0;
    //每小时 30 分执行
    //rule.minute = 30;
    //rule.second = 0;
    schedule.scheduleJob(rule, async () => {
        const check = await User.find();
        if (check.length > 0) {
            for (let i = 0; i < check.length; i++) {
                //console.log(check[i].hp);
                //console.log(check[i].max_hp);
                let uid = check[i]._id;
                let now_hp = check[i].hp;
                let max_hp = check[i].max_hp;
                if(now_hp < max_hp){// 先判断是不是满血
                    up = Math.floor(Math.floor(max_hp)*0.1); // 每次回复最大生命值的1%
                    reply_hp = now_hp + up; // 回复后血量
                    if(reply_hp>=max_hp){ // 判断会不会溢出
                        now_hp = max_hp; // 当前的血量就等于最大生命值了（满血）
                        updates = { $set: {hp: now_hp} };
                        await User.updateOne({ _id: uid }, updates, function (error) {
                            if (error) {
                                console.log(error);
                            } else {
                                console.log(check[i].name + "生命值自动回复,当前生命值：" + now_hp);
                            }
                        });
                    }else{
                        now_hp = reply_hp;
                        updates = { $set: {hp: now_hp} };
                        await User.updateOne({ _id: uid }, updates, function (error) {
                            if (error) {
                                console.log(error);
                            } else {
                                console.log(check[i].name + "生命值自动回复,当前生命值：" + now_hp);
                            }
                        });
                    }
                }
            };
        } else {
            console.log("自动回复方法出问题");
        };
    });
}

async function energyreply() {
    //每分钟的第分钟定时执行一次:
    let rule = new schedule.RecurrenceRule();
    rule.second = 0;
    //每小时 30 分执行
    //rule.minute = 30;
    //rule.second = 0;
    schedule.scheduleJob(rule, async () => {
        const check = await User.find();
        if (check.length > 0) {
            for (let i = 0; i < check.length; i++) {
                //console.log(check[i].energy);
                //console.log(check[i].max_energy);
                let uid = check[i]._id;
                let now_energy = check[i].energy;
                let max_energy = check[i].max_energy;
                if(now_energy < max_energy){// 先判断是不是满血
                    up = 5; // 每次回复最大生命值的1%
                    reply_energy = Math.floor(now_energy + up); // 回复后血量
                    if(reply_hp>=max_energy){ // 判断会不会溢出
                        now_energy = max_energy; // 当前的血量就等于最大生命值了（满血）
                        updates = { $set: {enery: now_energy} };
                        await User.updateOne({ _id: uid }, updates, function (error) {
                            if (error) {
                                console.log(error);
                            } else {
                                console.log(check[i].name + "体力自动回复,当前体力：" + now_energy);
                            }
                        });
                    }else{
                        now_energy = reply_hp;
                        updates = { $set: {energy: now_energy} };
                        await User.updateOne({ _id: uid }, updates, function (error) {
                            if (error) {
                                console.log(error);
                            } else {
                                console.log(check[i].name + "体力自动回复,当前体力：" + now_hp);
                            }
                        });
                    }
                }
            };
        } else {
            console.log("自动回复方法出问题");
        };
    });
}
module.exports = {
    hpreply: hpreply,
    energyreply: energyreply
};